package com.pug.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pug.product.dto.ProductPageRespDTO;
import com.pug.product.dto.ProductReqDTO;
import com.pug.product.dto.ProductRespDTO;
import com.pug.product.mapper.ProductMapper;
import com.pug.product.po.ProductPO;
import com.pug.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xq.pug.common.dto.PageDTO;

import java.util.List;

/**
 * @author RenGaoshuai
 * @date 2023/5/16 15:23
 * @description
 */
@Service
public class ProductServiceImpl implements IProductService {

    @Autowired
    private ProductMapper productMapper;

    @Override
    public List<ProductRespDTO> queryProducts(ProductReqDTO reqDTO) {
        LambdaQueryWrapper<ProductPO> wrapper = new LambdaQueryWrapper<>();
        List<ProductPO> poList = productMapper.selectList(wrapper);
        return BeanUtil.copyToList(poList, ProductRespDTO.class);

    }

    @Override
    public ProductPageRespDTO queryPage(PageDTO pageDTO) {
        //查询产品
        LambdaQueryWrapper<ProductPO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductPO::getIsDelete, 0);
        Page<ProductPO> page = new Page<>(pageDTO.getPageNum(), pageDTO.getPageSize());
        productMapper.selectPage(page, wrapper);

        List<ProductPO> poList = page.getRecords();

        ProductPageRespDTO respDTO = new ProductPageRespDTO();
        respDTO.setTotal((int)page.getTotal());
        respDTO.setPageNum(pageDTO.getPageNum());
        respDTO.setPageSize(pageDTO.getPageSize());
        List<ProductRespDTO> products = BeanUtil.copyToList(poList, ProductRespDTO.class);
        respDTO.setProducts(products);

        return respDTO;
    }
}
